package com.example.christina.service;
import com.example.christina.repository.OrdersRepository;
import com.example.christina.enity.Orders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrdersService {
    @Autowired
    private OrdersRepository ordersRepository;

    public void addOrders(Orders orders)
    {
        ordersRepository.save(orders);
    }

    //经查询资料，相同id会进行覆盖操作，以代替传统update，且传统update需要处理锁的问题
    public void updateOrders(Integer id)
    {
        Orders orders = findOrderById(id);
        orders.setIfSold();
    }

    public void deleteOrders(Orders orders)
    {
        ordersRepository.delete(orders);
    }

    public List<?> findAll()
    {
        return ordersRepository.findAll();
    }

    public Orders findOrderById(Integer id)
    {
        return ordersRepository.findByid(id);
    }

    public List<?> findOrderByUserName(String name)
    {
        return ordersRepository.findByuserName(name);
    }

    public void deleteOrderById(Integer id)
    {
        ordersRepository.deleteById(id);
    }
}
